package com.mengmablog.blog.controller;

import com.mengmablog.blog.commons.BlogConst;
import com.mengmablog.blog.commons.ServerResponse;
import com.mengmablog.blog.service.port.IArticlesService;
import com.mengmablog.blog.service.port.ICommentsService;
import com.mengmablog.blog.service.port.IMateaService;
import com.mengmablog.blog.service.port.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("api/admin")
public class MasterController {

    @Autowired
    private IArticlesService iArticlesService;

    @Autowired
    private ICommentsService iCommentsService;

    @Autowired
    private IUserService iUserService;

    @Autowired
    private IMateaService iMateaService;

    /**
     * 获取全部文章信息
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("article")
    public ServerResponse article(@RequestParam(required = false, defaultValue = BlogConst.PAGE) Integer page,
                                  @RequestParam(required = false, defaultValue = BlogConst.LIMIT) Integer limit) {
        return iArticlesService.article(page, limit);
    }

    /**
     * 根据id获取详细文章
     *
     * @param id
     * @return
     */
    @GetMapping("article/{id}")
    public ServerResponse article_id(@PathVariable String id) {
        return iArticlesService.article_id(Integer.valueOf(id), "master");
    }

    /**
     * 获取全部评论
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("comment")
    public ServerResponse comment(@RequestParam(required = false, defaultValue = BlogConst.PAGE) Integer page,
                                  @RequestParam(required = false, defaultValue = BlogConst.LIMIT) Integer limit) {
        return iCommentsService.admin_comment(page, limit);
    }

    /**
     * 获取评论个数
     *
     * @return
     */
    @GetMapping("comment/count")
    public ServerResponse comment_count() {
        return iCommentsService.comment_count();
    }

    /**
     * 获取文章个数
     *
     * @return
     */
    @GetMapping("article/count")
    public ServerResponse article_count() {
        return iArticlesService.article_count();
    }

    /**
     * 新增获取修改
     *
     * @param id
     * @param title
     * @param content
     * @param tags
     * @param category
     * @param status
     * @param allowComment
     * @return
     */
    @PostMapping("article")
    public ServerResponse article(@RequestParam(required = false, defaultValue = "0") Integer id,
                                  @RequestParam(required = true) String title,
                                  @RequestParam(required = false) String content,
                                  @RequestParam(required = false) String tags,
                                  @RequestParam(required = false) String category,
                                  @RequestParam(required = false) String status,
                                  @RequestParam(required = false) Boolean allowComment) {

        return iArticlesService.article(id, title, content, tags, category, status, allowComment);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("article/{id}")
    public ServerResponse article_id_delete(@PathVariable String id) {
        return iArticlesService.delete(id);
    }

    /**
     * 获取评论详细列表
     *
     * @param id
     * @return
     */
    @GetMapping("comment/{id}")
    public ServerResponse comment_id(@PathVariable Integer id) {
        return iCommentsService.comment_id(id);
    }

    /**
     * 登录
     * @param username
     * @param password
     * @param session
     * @return
     */
    @PostMapping("login")
    public ServerResponse login(@RequestParam(required = true) String username,
                                @RequestParam(required = true) String password,
                                HttpSession session) {
        ServerResponse response = iUserService.login(username, password);
        //判断是否成功
        if (response.isSuccess()) {
            session.setAttribute(BlogConst.CURRENT_USER, response.getData());
            return ServerResponse.createBySuccess();
        }
        return ServerResponse.createByError();
    }

    /**
     * 退出登录
     * @param session
     * @return
     */
    @PostMapping("logout")
    public ServerResponse logout(HttpSession session) {
        if (session != null) {
            session.removeAttribute(BlogConst.CURRENT_USER);
            return ServerResponse.createBySuccess();
        }
        return ServerResponse.createByError();

    }

    /**
     * 重置密码
     * @param username
     * @param oldPassword
     * @param newPassword
     * @return
     */
    @PostMapping("reset")
    public ServerResponse reset(@RequestParam(required = true)String username,
                                @RequestParam(required = true)String oldPassword,
                                @RequestParam(required = true)String newPassword){
        return iUserService.reset(username,oldPassword,newPassword);
    }


    /**
     * 删除评论
     * @param id
     * @return
     */
    @DeleteMapping("comment/{id}")
    public ServerResponse comment_id_delete(@PathVariable Integer id){
        return iCommentsService.delete(id);
    }

    /**
     * 获取所有属性
     * @param type
     * @return
     */
    @GetMapping("meta")
    public ServerResponse meta(String type){
        return iMateaService.meta(type);
    }

    /**
     * 根据name删除分类
     * @param type
     * @param name
     * @return
     */
    @DeleteMapping("meta")
    public ServerResponse meta(@RequestParam(required = true)String type,
                               @RequestParam(required = true)String name){
        return iMateaService.meta_delete(type,name);
    }

    /**
     * 添加一个分类
     * @param name
     * @param type
     * @return
     */
    @PostMapping("meta")
    public ServerResponse meta_insert(@RequestParam(required = true)String name ,
                                      @RequestParam(required = true)String type) {
        return iMateaService.meta_insert(name, type);
    }

    /**
     * 根据id修改分类
     * @param id
     * @param name
     * @param type
     * @return
     */
    @PostMapping("meta/{id}")
    public ServerResponse meta_id(@PathVariable String id,
                                  @RequestParam(required = true)String name,
                                  @RequestParam(required = true)String type){
        return iMateaService.meta_update(id,name,type);
    }

    /**
     * 自定义页面列表
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("page")
    public ServerResponse page(@RequestParam(required = false, defaultValue = BlogConst.PAGE) Integer page,
                               @RequestParam(required = false, defaultValue = BlogConst.LIMIT) Integer limit){
        return iArticlesService.page(page,limit);
    }

    /**
     * 获取自定义页面信息
     * @param id
     * @return
     */
    @GetMapping("page/{id}")
    public ServerResponse page_id(@PathVariable String id){
        return iArticlesService.page_id(id);
    }

    /**
     * 新建或修改自定义页面
     * @param id
     * @param title
     * @param content
     * @param status
     * @return
     */
    @PostMapping("page")
    public ServerResponse page_insert(@RequestParam(required = false,defaultValue = "0")Integer id,
                                      @RequestParam(required = true)String title,
                                      @RequestParam(required = true)String content,
                                      @RequestParam(required = true,defaultValue = "draft")String status){
        return iArticlesService.page_insert(id,title,content,status);
    }

    @DeleteMapping("page/{id}")
    public ServerResponse page_id(@PathVariable Integer id){
        return iArticlesService.page_delete(id);
    }
}
